package com.hongxun.project.assets.bByq.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.hongxun.common.utils.StringUtils;
import com.hongxun.framework.aspectj.lang.annotation.DataScope;
import com.hongxun.framework.web.controller.BaseController;
import com.hongxun.framework.web.domain.Ztree;
import com.hongxun.project.assets.Tools.StringZTree;
import com.hongxun.project.assets.iLine.domain.ILine;
import com.hongxun.project.assets.iLine.mapper.ILineMapper;
import com.hongxun.project.assets.iPoint.domain.IPoint;
import com.hongxun.project.assets.iPoint.service.IIPointService;
import com.hongxun.project.system.menu.domain.Menu;
import com.hongxun.project.system.role.domain.Role;
import com.hongxun.project.system.role.mapper.RoleMapper;
import com.hongxun.project.system.user.domain.User;
import com.hongxun.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hongxun.project.assets.bByq.mapper.BByqMapper;
import com.hongxun.project.assets.bByq.domain.BByq;
import com.hongxun.project.assets.bByq.service.IBByqService;
import com.hongxun.common.utils.text.Convert;

/**
 * 变压器 服务层实现
 * 
 * @author hongxun
 * @date 2019-03-19
 */
@Service
public class BByqServiceImpl implements IBByqService 
{
	@Autowired
	private BByqMapper bByqMapper;
	@Autowired
	private ILineMapper iLineMapper;
	@Autowired
	private IIPointService iiPointService;
	@Autowired
	private IUserService userService;
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private IBByqService bByqService;

//	private static final boolean flag = false;

	/**
     * 查询变压器信息
     * 
     * @param id 变压器ID
     * @return 变压器信息
     */
    @Override
	public BByq selectBByqById(String id)
	{
	    return bByqMapper.selectBByqById(id);
	}
	
	/**
     * 查询变压器列表
     * 
     * @param bByq 变压器信息
     * @return 变压器集合
     */
	@Override
	@DataScope(tableAlias = "u")
	public List<BByq> selectBByqList(BByq bByq)
	{
	    return bByqMapper.selectBByqList(bByq);
	}
	
    /**
     * 新增变压器
     * 
     * @param bByq 变压器信息
     * @return 结果
     */
	@Override
	public int insertBByq(BByq bByq)
	{
		bByq.setId(UUID.randomUUID().toString());
	    return bByqMapper.insertBByq(bByq);
	}
	
	/**
     * 修改变压器
     * 
     * @param bByq 变压器信息
     * @return 结果
     */
	@Override
	public int updateBByq(BByq bByq)
	{
	    return bByqMapper.updateBByq(bByq);
	}

	/**
     * 删除变压器对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteBByqByIds(String ids)
	{
		return bByqMapper.deleteBByqByIds(Convert.toStrArray(ids));
	}


	@Override
	public int countAll(BByq bByq){
		return bByqMapper.countAll(bByq);
	}
	@Override
	public int count(String name , Long userId){
		return bByqMapper.count(name,userId);
	}


	@Override
	public List<BByq> limit(String name , int page){
		return bByqMapper.limit(name , page);
	}
	@Override
	public List<BByq> limitAll(String name ,Long userId , int page){
		return bByqMapper.limitAll(name ,userId, page);
	}

	@Override
	public List<Role> selectRolesByUserId(Long userId){
		return roleMapper.selectRolesByUserId(userId);
	}


	@Override
	public List<BByq> selectByqToUserId(Long userId , String name , Date runDate){
		return bByqMapper.selectByqToUserId(userId , name , runDate);
	}

	/**
	 * 查询所有变压器
	 *
	 * @return 变压器列表
	 */
	@Override
	public List<StringZTree> byqTreeData(String name , int page)
//	public List<StringZTree> byqTreeData(BByq bByq)
	{
		//	查询出所有变压器
//		List<BByq> bByqList = bByqMapper.selectBByqList( bByq);

		int pageNow = 1;	//	默认当前页为第一页
		int pageNum = page*15-15;// 当前页
		int pageTotal = 0;// 总条数
		int pageSize = 10;// 每页显示条数
		int pageLast = 0;//	 末页
		pageLast = Math.round(pageTotal/pageSize ) + (pageTotal % pageSize == 0 ? 0:1);

//		BByq bByq = new BByq();
//		bByq.setName(name);
//		List<BByq> bByqList1 = bByqMapper.selectBByqList(bByq);
//		pageTotal = bByqList1.size();
		List<BByq> bByqList = new ArrayList<>();
//		bByqList = limit(user.getDeptId().toString(),name,pageNum);
		if(new BaseController().getUserId() ==1)
			bByqList = limit(name,pageNum);
		else
			bByqList = limitAll(name,new BaseController().getUserId(),pageNum);
		User user = userService.selectUserById(new BaseController().getUserId());
		List<Role> roleList = bByqService.selectRolesByUserId(new BaseController().getUserId());

//		for (Role role : roleList) {
//			if(role.getRemark()!= null && role.getRemark() != ""){
//				if(!role.getRemark().equals("管理员"))
////					bByq.setDeptId(user.getDeptId().toString());
//					bByqList = limit(user.getDeptId().toString(),name,pageNum);
//			}
//		}

//		List<BByq> bByqList = limit(user.getDeptId().toString() ,name,pageNum);
		//	查询出所有的线路
		List<ILine> iLineList = iLineMapper.selectILineList(new ILine());
		//	对线路进行排序
//		List<ILine> iLineList_1 = new ArrayList<>();
//		for (ILine iLine : iLineList) {
//			for (ILine line : iLineList) {
//				if (iLine.getId().equals(line.getUpLineId())){
//					iLineList_1.add(line);
//				}
//			}
//		}
		//	加载变压器列表树--循环加载下级线路列表
		List<StringZTree> ztrees = initZtree(bByqList,iLineList,name);
		return ztrees;
	}



	/**
	 * 对象转菜单树
	 *
	 * @param
	 * @return 树结构列表
	 */
	public List<StringZTree> initZtree(List<BByq> bByqList , List<ILine> iLineList , String name)
	{
		return initZtree(bByqList, iLineList,name,null, false);
	}



	/**
	 * 对象转菜单树
	 *
	 * @param
	 * @param
	 * @param permsFlag 是否需要显示权限标识
	 * @return 树结构列表
	 */
	/*public List<StringZTree> initZtree(List<BByq> bByqList,List<ILine> iLineList , List<String> roleMenuList, boolean permsFlag)
	{
		List<StringZTree> ztrees = new ArrayList<StringZTree>();
		boolean isCheck = StringUtils.isNotNull(roleMenuList);
		int count = bByqMapper.count(new BByq());

		//	变压器tree
		for (BByq bByq : bByqList)
		{
			// TODO: 2019/3/27  变压器节点的parent_id设置为0，设置的变压器的节点id为下级线路的parent_id，进行环环相扣
			StringZTree ztree = new StringZTree();
			ztree.setId(bByq.getId());					// 1
			ztree.setpId("0");		//0
			ztree.setOpen(false);
			ztree.setCount(count);
			ztree.setPage(0);
			ztree.setPageSize(15);
			ztree.setName(transMenuName(bByq, roleMenuList, permsFlag));
			ztree.setTitle(bByq.getName());


			// 	下级线路Tree列表
			// TODO: 2019/3/27  通过变压器ID查询出所属该变压器的下级线路,线路的父ID就是变压器的ID
			//	变压器下辖低压主线
			for (ILine iLine : iLineList) {
				if (iLine.getByqId().equals(bByq.getId()) ) {
					//	当上级线路为空时，次线路即为一级线路
						StringZTree ztreeLineGen = new StringZTree();
					if(iLine.getUpLineId() == null || iLine.getUpLineId().equals("")){
						ztreeLineGen.setId(iLine.getId());            //11
						ztreeLineGen.setpId(ztree.getId());    //1
						ztreeLineGen.setOpen(false);
						ztreeLineGen.setName(iLine.getName());
						ztreeLineGen.setTitle(iLine.getName());


						//	低压主线下辖低压分支线
						for (ILine line : iLineList) {
							if (iLine.getId().equals(line.getUpLineId())) {
								StringZTree ztreeLineZi = new StringZTree();
								ztreeLineZi.setId(line.getId());                    //	111
								ztreeLineZi.setpId(ztreeLineGen.getId());    //	11
								ztreeLineZi.setOpen(false);
								ztreeLineZi.setName(line.getName());
								ztreeLineZi.setTitle(line.getName());

//								for (ILine line1 : iLineList) {
//									if (line.getId().equals(line1.getUpLineId())) {
//										StringZTree ztreeLineZi1 = new StringZTree();
//										ztreeLineZi1.setId(line1.getId());                    //	111
//										ztreeLineZi1.setpId(ztreeLineZi.getId());    //	11
//										ztreeLineZi1.setOpen(false);
//										ztreeLineZi1.setName(line1.getName());
//										ztreeLineZi1.setTitle(line1.getName());
//
//										ztrees.add(ztreeLineZi1);
//									}
//								}

								ztrees.add(ztreeLineZi);
							}
						}
						ztrees.add(ztreeLineGen);
					}
				}
			}

			ztrees.add(ztree);
//			index++;
		}
		return ztrees;
	}*/



	public List<StringZTree> initZtree(List<BByq> bByqList,List<ILine> iLineList ,String name , List<String> roleMenuList, boolean permsFlag)
	{
		List<StringZTree> ztrees = new ArrayList<StringZTree>();
//		boolean isCheck = StringUtils.isNotNull(roleMenuList);
//		User user = userService.selectUserById(new BaseController().getUserId());
		/*BByq bByqs = new BByq();
		List<Role> roleList = bByqService.selectRolesByUserId(new BaseController().getUserId());
		for (Role role : roleList) {
			if(role.getRemark()!= null && role.getRemark() != ""){
				if(!role.getRemark().equals("管理员"))
					bByqs.setDeptId(user.getDeptId().toString());
			}
		}


		bByqs.setName(name);*/
//		bByqs.setDeptId(user.getDeptId().toString());
//		int count = bByqMapper.count(bByqs);
		int count = 0;
		if(new BaseController().getUserId() !=1)
			count = bByqMapper.count(name,new BaseController().getUserId());
		else
			count = bByqMapper.countAll(new BByq());

		//	变压器tree
		for (BByq bByq : bByqList)
		{
			IPoint iPoint = iiPointService.selectIPointById(bByq.getPointId());
			// TODO: 2019/3/27  变压器节点的parent_id设置为0，设置的变压器的节点id为下级线路的parent_id，进行环环相扣
			StringZTree ztree = new StringZTree();
			ztree.setId(bByq.getId());					// 1
			ztree.setpId("0");		//0
			ztree.setOpen(false);
			ztree.setCount(count);
			ztree.setPage(0);
			ztree.setPageSize(15);
			if(iPoint != null){
				ztree.setLgt(iPoint.getLgt());
				ztree.setLat(iPoint.getLat());
			}
			ztree.setName(transMenuName(bByq, roleMenuList, permsFlag));
			ztree.setTitle(bByq.getName());


			// 	下级线路Tree列表
			// TODO: 2019/3/27  通过变压器ID查询出所属该变压器的下级线路,线路的父ID就是变压器的ID
			//	变压器下辖低压主线
			for (ILine iLine : iLineList) {
				if (iLine.getByqId().equals(bByq.getId()) ) {
					//	当上级线路为空时，次线路即为一级线路
						StringZTree ztreeLineGen = new StringZTree();
					if(iLine.getUpLineId() == null || iLine.getUpLineId().equals("")){
						ztreeLineGen.setId(iLine.getId());            //11
						ztreeLineGen.setpId(ztree.getId());    //1
						ztreeLineGen.setOpen(false);
						ztreeLineGen.setName(iLine.getName());
						ztreeLineGen.setTitle(iLine.getName());


						//	低压主线下辖低压分支线
						forLineTree(iLine,ztrees,ztreeLineGen,iLineList);

						ztrees.add(ztreeLineGen);
					}
				}
			}

			ztrees.add(ztree);
//			index++;
		}
		return ztrees;
	}



	public String transMenuName(BByq bByq, List<String> roleMenuList, boolean permsFlag)
	{
		StringBuffer sb = new StringBuffer();
		sb.append(bByq.getName());
		if (permsFlag)
		{
//			sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + bByq.getPerms() + "</font>");
		}
		return sb.toString();
	}


	/**
	 * 	循环遍历子线路
	 * @param
	 * @param iLineList
	 * @return
	 */
	public StringZTree forLineTree(ILine iLine ,List<StringZTree> ztrees ,StringZTree ztreeLineGen ,List<ILine> iLineList){
		StringZTree sZTree = new StringZTree();

		//	低压主线下辖低压分支线
		for (ILine line : iLineList) {
			if (iLine.getId().equals(line.getUpLineId())) {
				StringZTree ztreeLineZi = new StringZTree();
				ztreeLineZi.setId(line.getId());                    //	111
				ztreeLineZi.setpId(ztreeLineGen.getId());    //	11
				ztreeLineZi.setOpen(false);
				ztreeLineZi.setName(line.getName());
				ztreeLineZi.setTitle(line.getName());

				forSonLineTree(line,ztreeLineZi ,ztrees,ztreeLineGen,iLineList);

				ztrees.add(ztreeLineZi);
			}
		}


		return sZTree;
	}


	public StringZTree forSonLineTree(ILine line ,StringZTree ztreeLineZi ,List<StringZTree> ztrees ,StringZTree ztreeLineGen ,List<ILine> iLineList ){
		StringZTree sZTree = new StringZTree();

		for (ILine line1 : iLineList) {
			if (line.getId().equals(line1.getUpLineId())) {
 				StringZTree ztreeLineZi1 = new StringZTree();
				ztreeLineZi1.setId(line1.getId());                    //	111
				ztreeLineZi1.setpId(ztreeLineZi.getId());    //	11
				ztreeLineZi1.setOpen(false);
				ztreeLineZi1.setName(line1.getName());
				ztreeLineZi1.setTitle(line1.getName());

				forSonLineTree1(line1,ztreeLineZi1 ,ztrees,ztreeLineGen,iLineList);

				ztrees.add(ztreeLineZi1);
			}
		}

//		ztrees.add(ztreeLineZi);

		return sZTree;
	}

	public StringZTree forSonLineTree1(ILine line ,StringZTree ztreeLineZi ,List<StringZTree> ztrees ,StringZTree ztreeLineGen ,List<ILine> iLineList ){
		StringZTree sZTree = new StringZTree();

		for (ILine line1 : iLineList) {
			if (line.getId().equals(line1.getUpLineId())) {
				StringZTree ztreeLineZi2 = new StringZTree();
				ztreeLineZi2.setId(line1.getId());                    //	111
				ztreeLineZi2.setpId(ztreeLineZi.getId());    //	11
				ztreeLineZi2.setOpen(false);
				ztreeLineZi2.setName(line1.getName());
				ztreeLineZi2.setTitle(line1.getName());

				forSonLineTree2(line1,ztreeLineZi2 ,ztrees,ztreeLineGen,iLineList);

				ztrees.add(ztreeLineZi2);
			}
		}

//		ztrees.add(ztreeLineZi);

		return sZTree;
	}


	public StringZTree forSonLineTree2(ILine line ,StringZTree ztreeLineZi ,List<StringZTree> ztrees ,StringZTree ztreeLineGen ,List<ILine> iLineList ){
		StringZTree sZTree = new StringZTree();

		for (ILine line1 : iLineList) {
			if (line.getId().equals(line1.getUpLineId())) {
				StringZTree ztreeLineZi1 = new StringZTree();
				ztreeLineZi1.setId(line1.getId());                    //	111
				ztreeLineZi1.setpId(ztreeLineZi.getId());    //	11
				ztreeLineZi1.setOpen(false);
				ztreeLineZi1.setName(line1.getName());
				ztreeLineZi1.setTitle(line1.getName());

				forSonLineTree3(line1,ztreeLineZi1 ,ztrees,ztreeLineGen,iLineList);

				ztrees.add(ztreeLineZi1);
			}
		}

//		ztrees.add(ztreeLineZi);

		return sZTree;
	}



	public StringZTree forSonLineTree3(ILine line ,StringZTree ztreeLineZi ,List<StringZTree> ztrees ,StringZTree ztreeLineGen ,List<ILine> iLineList ){
		StringZTree sZTree = new StringZTree();

		for (ILine line1 : iLineList) {
			if (line.getId().equals(line1.getUpLineId())) {
				StringZTree ztreeLineZi1 = new StringZTree();
				ztreeLineZi1.setId(line1.getId());                    //	111
				ztreeLineZi1.setpId(ztreeLineZi.getId());    //	11
				ztreeLineZi1.setOpen(false);
				ztreeLineZi1.setName(line1.getName());
				ztreeLineZi1.setTitle(line1.getName());

				forSonLineTree4(line1,ztreeLineZi1 ,ztrees,ztreeLineGen,iLineList);

				ztrees.add(ztreeLineZi1);
			}
		}

//		ztrees.add(ztreeLineZi);

		return sZTree;
	}



	public StringZTree forSonLineTree4(ILine line ,StringZTree ztreeLineZi ,List<StringZTree> ztrees ,StringZTree ztreeLineGen ,List<ILine> iLineList ){
		StringZTree sZTree = new StringZTree();

		for (ILine line1 : iLineList) {
			if (line.getId().equals(line1.getUpLineId())) {
				StringZTree ztreeLineZi1 = new StringZTree();
				ztreeLineZi1.setId(line1.getId());                    //	111
				ztreeLineZi1.setpId(ztreeLineZi.getId());    //	11
				ztreeLineZi1.setOpen(false);
				ztreeLineZi1.setName(line1.getName());
				ztreeLineZi1.setTitle(line1.getName());

				forSonLineTree5(line1,ztreeLineZi1 ,ztrees,ztreeLineGen,iLineList);

				ztrees.add(ztreeLineZi1);
			}
		}

//		ztrees.add(ztreeLineZi);

		return sZTree;
	}



	public StringZTree forSonLineTree5(ILine line ,StringZTree ztreeLineZi ,List<StringZTree> ztrees ,StringZTree ztreeLineGen ,List<ILine> iLineList ){
		StringZTree sZTree = new StringZTree();

		for (ILine line1 : iLineList) {
			if (line.getId().equals(line1.getUpLineId())) {
				StringZTree ztreeLineZi1 = new StringZTree();
				ztreeLineZi1.setId(line1.getId());                    //	111
				ztreeLineZi1.setpId(ztreeLineZi.getId());    //	11
				ztreeLineZi1.setOpen(false);
				ztreeLineZi1.setName(line1.getName());
				ztreeLineZi1.setTitle(line1.getName());

				forSonLineTree6(line1,ztreeLineZi1 ,ztrees,ztreeLineGen,iLineList);

				ztrees.add(ztreeLineZi1);
			}
		}

//		ztrees.add(ztreeLineZi);

		return sZTree;
	}



	public StringZTree forSonLineTree6(ILine line ,StringZTree ztreeLineZi ,List<StringZTree> ztrees ,StringZTree ztreeLineGen ,List<ILine> iLineList ){
		StringZTree sZTree = new StringZTree();

		for (ILine line1 : iLineList) {
			if (line.getId().equals(line1.getUpLineId())) {
				StringZTree ztreeLineZi1 = new StringZTree();
				ztreeLineZi1.setId(line1.getId());                    //	111
				ztreeLineZi1.setpId(ztreeLineZi.getId());    //	11
				ztreeLineZi1.setOpen(false);
				ztreeLineZi1.setName(line1.getName());
				ztreeLineZi1.setTitle(line1.getName());

				ztrees.add(ztreeLineZi1);
			}
		}

//		ztrees.add(ztreeLineZi);

		return sZTree;
	}


}
